package com.max.cms0.web.contorller;

import com.max.cms0.entity.User;
import com.max.cms0.entity.VO.LoginParam;
import com.max.cms0.result.Result;
import com.max.cms0.service.impl.UserServiceImpl;
import com.max.cms0.utils.JwtUtil;
import com.max.cms0.utils.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: cms0
 * @BelongsPackage: com.max.cms0.web.contorller
 * @Author: MHN
 * @CreateTime: 2024-11-21  09:44
 * @Description: TODO
 * @Version: 1.0
 */
@Api(tags = "登录模块")
@RestController
public class LoginController {
    @Autowired
    private UserServiceImpl userService;
    @ApiOperation(value = "登录")
    @PostMapping(value = "/login")
    public Result login(@RequestBody LoginParam param) {
    //对密码加密与数据库进行比对
        User user = userService.login(param.getUsername(),
                MD5Utils.MD5(param.getPassword()));
        //新增token 相关代码
        Map<String,Object> map = new HashMap<>();
        map.put("userId",user.getId());
        map.put("username", user.getUsername());
        // 放入isVip不合适，后期充值成功后，用户不会重新登录，token值不变，导致isVip值是错误的
        map.put("isVip", user.getIsVip());
        map.put("roleId", user.getRoleId());
        String token = JwtUtil.generateJwt(map);
        return Result.success(token);
    }

    @ApiOperation(value = "退出登录")
    @PostMapping(value = "/logout")
    public Result logout(){
        return Result.success();
    }


}
